﻿Imports System.IO
Public Class Form1
    Const beginLable = "C_"
    Private DEF_APP_TITLEBAR = "Replace Error"
    Private AppTitle = "Replace Commas"
    Private beWrittenString As String = ""
    Private pStreamReader As StreamReader
    Private pStreamWriter As StreamWriter
    Private pTargetFileName As String = ""
    Private pTargetFilePath As String = ""
    Private workPath As String
    Private outPutPath As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        workPath = Application.StartupPath
        outPutPath = workPath + "\Out\"
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim tempString As String = ""
        Dim arrayFiels As String()
        Dim Converd As Boolean = False
        Try
            If String.Empty.Equals(txtInputPath.Text.Trim()) Then
                MessageBox.Show("Please choose a folder first", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Stop)
                btnBrowser.Focus()
                Exit Sub
            End If
            If checkDiretory(txtInputPath.Text) Then
                'folder is exist
                'get all files in the folder
                arrayFiels = Directory.GetFiles(txtInputPath.Text.Trim)
                For i As Integer = 0 To arrayFiels.Length - 1
                    'if it is never be converted
                    If Not Path.GetFileName(arrayFiels(i)).StartsWith(beginLable) Then
                        'create the new reader
                        pStreamReader = New StreamReader(arrayFiels(i))
                        'the putout path doesn't exist,then cereate it.
                        If Not Directory.Exists(outPutPath) Then
                            Directory.CreateDirectory(outPutPath)
                        End If
                        'new the writer object
                        pStreamWriter = New StreamWriter(outPutPath + Path.GetFileName(arrayFiels(i)))
                        'pStreamWriter = New StreamWriter(pTargetFileName + Path.GetFileName(arrayFiels(i)))
                        'read file
                        While (True)
                            tempString = ReadLine()
                            If tempString = Nothing Then
                                Exit While
                            End If
                            beWrittenString = tempString.Replace(",", vbTab)
                            WriteLine(beWrittenString)
                        End While
                        ReaderClose()
                        WriterClose()
                        'at lease one file has been convered then set the flag to be true
                        Converd = True
                        File.Copy(arrayFiels(i), pTargetFileName + "\" + beginLable + Path.GetFileName(arrayFiels(i)), True)
                        File.Delete(arrayFiels(i))
                    End If
                Next
                If Converd Then
                    lblResult.Text = "The Converted file(s) has been placed in  " + outPutPath
                    MessageBox.Show("Replace Successful", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    MessageBox.Show("No files found to convert", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If

            End If


        Catch ex As Exception
            MessageBox.Show(ex.Message, DEF_APP_TITLEBAR, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub
    Private Function checkDiretory(ByVal path As String) As Boolean
        If Directory.Exists(path) Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Function ReadLine() As String
        Try
            Return pStreamReader.ReadLine
        Catch ex As Exception
            'MessageBox.Show("The file could not be read.")
            MessageBox.Show(ex.Message, DEF_APP_TITLEBAR, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function WriteLine(ByVal line As String) As Boolean
        Try
            pStreamWriter.WriteLine(line)
            ' MessageBox.Show(line)
            pStreamWriter.Flush()
            Return True
        Catch ex As Exception
            'MessageBox.Show("The file could not be written.")
            MessageBox.Show(ex.Message, DEF_APP_TITLEBAR, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Public Function ReaderClose() As Boolean
        Try
            'pStreamWriter.Close()
            If Not pStreamReader Is Nothing Then pStreamReader.Close()
            Return True
        Catch ex As Exception
            'MessageBox.Show("The file could not be closed.")
            MessageBox.Show(ex.Message, DEF_APP_TITLEBAR, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function


    Public Function WriterClose() As Boolean
        Try
            pStreamWriter.Close()
            'pStreamReader.Close()
            Return True
        Catch ex As Exception
            'MessageBox.Show("The file could not be closed.")
            MessageBox.Show(ex.Message, DEF_APP_TITLEBAR, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowser.Click

        If FolderBrowserDialog1.ShowDialog(Me) = DialogResult.OK Then
            txtInputPath.Text = FolderBrowserDialog1.SelectedPath
            pTargetFileName = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class
